package com.bdth.weget.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.weget.common.Pagination;
import com.bdth.weget.entity.CarUnrule;
import com.bdth.weget.mapper.CarUnruleMapper;
import com.bdth.weget.service.CarUnruleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆违章记录 服务实现类
 * </p>
 *
 * @author LGP
 * @since 2019-07-10
 */
@Service
public class CarUnruleServiceImpl extends ServiceImpl<CarUnruleMapper, CarUnrule> implements CarUnruleService {
    @Autowired
    private CarUnruleMapper carUnruleMapper;
    @Override
    public Page<CarUnrule> listPages(Pagination pagination, Map<String, Object> query) {
        EntityWrapper<CarUnrule> entityWrapper = new EntityWrapper<>();
        if(query.get("carids")!=null)
            entityWrapper.in("carid",(String) query.get("carids"));
        if(query.get("plateno")!=null)
            entityWrapper.like("plateno",(String) query.get("plateno"));
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<CarUnrule> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        List<CarUnrule> items = carUnruleMapper.selectPage(page,entityWrapper);
        page.setRecords(items);
        return page;
    }

    public Integer listTotal(Map<String, Object> query) {
        EntityWrapper<CarUnrule> entityWrapper = new EntityWrapper<>();
        if(query.get("carids")!=null)
            entityWrapper.in("carid",(String) query.get("carids"));
        if(query.get("plateno")!=null)
            entityWrapper.like("plateno",(String) query.get("plateno"));
        Integer counts = carUnruleMapper.selectCount(entityWrapper);
        return counts;
    }

    @Override
    public boolean update(CarUnrule bean) {
        if(bean.getId()>0) {
            Integer result = carUnruleMapper.updateById(bean);
            if(result>0)
                return true;
        }
        return false;
    }

    @Override
    public boolean save(CarUnrule bean) {
        Integer result = carUnruleMapper.insert(bean);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        Integer result = carUnruleMapper.deleteById(id);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean remove(String ids) {
        String[] id = ids.split(",");
        Integer result = 0;
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            Integer r = carUnruleMapper.deleteById(intId);
            if(r>0)
                result++;
        }
        if(result>0)
            return true;
        return false;
    }
}
